<?

	include("inc/config.inc.php");

	include("inc/header.inc.php");

	echo "<h2><span class='textColorChange'>$config[systemtitle]</span></h2><br/>\n\n";

        echo "<a href='main.php' style='color:black;font-size:10pt'><< Back</a><br/>";


 	$u = mysqli_real_escape_string($con,$_GET[u]);

	// Check we are HR!
	if ($_SESSION[role] != "HR")
		die ("Error 61 - User error - Please contact support.");

        echo "<div style='background:#504E52' class='normalText' width='400'>\n";
	echo "<b>View User</b><br/>\n";
	echo "<form action='userview.php' method='GET'>\n";
	echo "Select user to view: <select name='u' onchange='this.form.submit();'><option value=''></option>\n";
	
	$result = mysqli_query($conn, "SELECT * FROM users WHERE deleted=0 and prestaged=0 ORDER BY user");
	
	while ($usr = mysqli_fetch_array($result))
		if ($usr[user] == $u)
			echo "<option selected value='$usr[user]'>$usr[user]</option>\n";
		else
			echo "<option value='$usr[user]'>$usr[user]</option>\n";
	
	echo "</select>\n";
	echo "</div></form>\n";
	echo "\n<br/>\n\n";
                

 // ---------- Entitlement Section ----------------------------------------------------------------------

		if ($u != "")
		{
			$r = mysqli_query($conn, "SELECT * FROM users WHERE user='$u'");
			$user = mysqli_fetch_array($r);

			$daysUsedThis = 0;
			$daysUsedNext = 0;
	
			// Work out total entitlement
			$total = $user[carriedover] + $user[entitlement];
			$total = number_format($total, 1, '.', '');
	
			// Get days used for this year and next
			$daysUsedThis = daysUsed(date("Y"), $u);
			$daysUsedNext = daysUsed(date("Y", strtotime("+1 year")), $u);
			
			// Work out remaining days for this year and next
			$daysRemainThis = $total-$daysUsedThis;
			$daysRemainNext = $user[entitlement]-$daysUsedNext;
	
			$thisyear = date("Y");
	                $nextyear = date("Y")+1;

                	echo "<div style='background:#504E52' class='normalText'>\n";
                	echo "<b>Holiday entitlement - $user[firstname] $user[lastname]</b>\n";
                	echo "<br/><div width='100%' align='center'><table width='95%' cellspacing='10' border='0'>\n";
                	echo "<tr><td width='50%' align='center'><div style='position:relative;left:-55px;top:10px'>$thisyear:</div><div style='line-height:1em;font-size:70px'>$daysRemainThis</div><div style='position:relative;left:70px;'>of $total remaining</div></td>\n";
                	echo "<td width='50%' align='center'><div style='position:relative;left:-55px;top:10px'>$nextyear:</div><div style='line-height:1em;font-size:70px'>$daysRemainNext</div><div style='position:relative;left:70px'>of $user[entitlement] remaining</div></td></tr>\n";
                	echo "</table><br/>\n";
                	echo "Your total annual entitlement is made up from $user[entitlement] days, plus $user[carriedover] carried over from last year.\n";
        	        echo "</div></div>\n";
	                echo "\n<br/>\n\n";


	
 	// ---------- Holidays Section -------------------------------------------------------------------------
	
			echo "<div style='background:#504E52' class='normalText'>\n";
			echo "<b>Holiday Requests for $user[firstname] $user[lastname]</b><br/><br/>\n";
	
			// TODO limit SQL to only get previous yearsperiods past requests, so we dont end up with loads of lines
			$reqResult = mysqli_query($conn, "SELECT * FROM requests WHERE user='$user[user]' OR user='' ORDER BY startdate DESC");
		
			if (mysqli_num_rows($reqResult) > 0)
			{
	
				echo "<table BORDERCOLORLIGHT='#807E7C' BORDERCOLORDARK='#383634' border='1'><tr><td><b>Description</b></td><td><b>Start Date</b></td><td><b>End Date</b></td><td><b>Days</b></td><td><b>Status</b></td><td><b>Info</b></td><td><b>Cancel</b></td></td></tr>";
	
				while ($request = mysqli_fetch_array($reqResult))
				{
						// Convert DB am/pm into times to make more logical for humans
					if ($request[starttime] == am)
						$request[starttime] = "09:00";
					if ($request[starttime] == pm)
						$request[starttime] = "12:00";
					if ($request[endtime] == am)
						$request[endtime] = "12:00";
					if ($request[endtime] == pm)
						$request[endtime] = "17:00";
		
						// Set status text colours
					if ($request[status] == "Pending")
						$pColor = "blue";
					if ($request[status] == "Accepted")
						$pColor = "green";
					if ($request[status] == "Rejected")
						$pColor = "red";
					if ($request[status] == "Cancelled")
						$pColor = "red";
		
					// Highlight newly added request
					if (	$_GET[newreq]==$request[requestid] OR $_GET[delreq]==$request[requestid])
						$bg = " bgcolor='orange'";
						else	
					$bg 	= "";
		
					echo "<tr$bg>\n";
					echo  "<td>$request[description] &nbsp;</td>\n";
					echo "<td>" . date("d/m/Y", strtotime($request[startdate])) . " $request[starttime]</td>\n";
					echo "<td>" . date("d/m/Y", strtotime($request[enddate])) . " $request[endtime]</td>\n";
					echo  "<td>" . str_replace(".0","",$request[days]) . "</td>\n";
					echo "<td style='color:$pColor'><b>$request[status]</b></td>\n";
					echo "<td>$request[statusmessage]</td>\n";

					if ($request[status] == "Cancelled" OR $request[statusmessage] == "Compulsory Holiday")
						echo "<td align='center'><img src='img/nodelete.png' width='16' height='16'></td></tr>\n";
					else		
						echo "<td align='center'><a href='cancel.php?hr=1&id=$request[requestid]' onClick='return confirmDelete(\"cancel\");'><img border='0' src='img/delete.png' width='16' height='16'></a></td></tr>\n";
		
				}
			
				echo "</table>";
			}	
			
			else
				echo "<i>$user[firstname] $user[lastname] has no holiday requests in the system. </i>";
		
			echo "</div>\n";
	
		}
	
		include("inc/footer.inc.php");
	
	?>
	
